<?php
/**
 * Created by PhpStorm.
 * BuyUser: USER
 * Date: 2018/11/12
 * Time: 10:56
 */

namespace common\sources\read\buyuser;

use Yii;
use common\models\craftsman\BuyUser;
use common\models\craftsman\SmsCode;

class BuyUserAccess implements IBuyUserAccess
{


    public function getBuyUser($phone)
    {
        return BuyUser::find()
            ->select('id,password,role')
            ->where(['phone'=>$phone])
            ->asArray()
            ->one();
    }

    public function getBuyUserByPhone($phone)
    {
        return BuyUser::find()
            ->select("id,phone,password,qq,wx,area,addr,role,is_pay,paypwd,reg_time,expire_time,pid,is_first")
            ->where(['phone' => $phone])
            ->asArray()
            ->one();
    }
    public function getBuyUserInfo($buy_Id)
    {
        return BuyUser::find()
            ->select("id,phone,password,qq,wx,area,addr,role,is_pay,paypwd,reg_time,expire_time,pid,is_first")
            ->where(['id'=>$buy_Id])
            ->asArray()
            ->one();
    }

    public function getChildrentCount($data)
    {
        $where = " pid = ".$data['buy_id'];
        $sql = "SELECT from_unixtime(reg_time, '%Y-%m-%d') AS `days`, count(id) as count  from buyuser WHERE  ".$where." GROUP BY `days` ORDER BY `days` DESC  ";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    public function getChildrens($data)
    {
        $where = " pid = ".$data['buy_id'];
        $sql = "SELECT from_unixtime(reg_time, '%Y-%m-%d') AS `days`, count(id) as count  from buyuser WHERE  ".$where." GROUP BY `days` ORDER BY `days` DESC  LIMIT ".($data['page_num']-1)* $data['limit']." ,".$data['limit'];
        return Yii::$app->db->createCommand($sql)->queryAll();
    }

    public function getInviteList($data)
    {
        return BuyUser::find()
                ->select("phone,role")
                ->where("pid = ".$data['buy_id']." AND from_unixtime(reg_time, '%Y-%m-%d') = '".$data['reg_day']."'")
                ->asArray()
                ->all();
    }

    public function getInviters($data)
    {
        return BuyUser::find()
                ->select("id,phone")
                ->where(['pid'=> $data['buy_id'],"role" => $data['role']])
                ->asArray()
                ->all();
    }

    public function getInviteCount($data)
    {
        return BuyUser::find()
                ->where(" pid =".$data['buy_id']." AND role =".$data['role'])
                ->count();
    }

    public function getInviteMonthCount($data)
    {
        return BuyUser::find()
                ->where(" pid =".$data['buy_id']." AND role =".$data['role']." AND reg_time>=".$data['month_time'])
                ->count();
    }

}